package com.shoujifeng.snowmusic.player;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.shoujifeng.snowmusic.adapter.ConllectAdapter;
import com.shoujifeng.snowmusic.adapter.OnlineListAdapter;
import com.shoujifeng.snowmusic.db.DatabaseUtil;
import com.shoujifeng.snowmusic.player.R;
import com.shoujifeng.snowmusic.player.http.ServerAccess;
import com.shoujifeng.snowmusic.player.interfaces.MoreInterface;
import com.shoujifeng.snowmusic.player.utils.MusicPlayer;

import android.R.array;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/**
 * 我收藏的单曲
 * @author luo
 *
 */
public class MyCollectSingleActivity  extends Activity {
	private ListView listView;
	private RelativeLayout headView;
	private TextView headText;
	private View lineLay;
	private ImageButton breakBtn;
	private int mPage = 0;

	private ConllectAdapter mAdapter;
	
	//数据
	private ArrayList<HashMap<String, Object>> mDataList;
	private ArrayList<HashMap<String, Object>> mTmpList;
	private int index;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_collect_single);
		init();
		
		getConllect();
	}

	public void init() {
		
		listView = (ListView) findViewById(R.id.my_collect_single_list);
		headView = (RelativeLayout) findViewById(R.id.headLay);
		headText = (TextView) headView.findViewById(R.id.head_text);
		breakBtn = (ImageButton) headView.findViewById(R.id.breakBtn);
		lineLay = LayoutInflater.from(MyCollectSingleActivity.this).inflate(R.layout.line_layout, null);
		
		listView.addFooterView(lineLay);
		breakBtn.setOnClickListener(onClickListener);
		listView.setDivider(null);
		headText.setText("我收藏的单曲");
		
		setListView();
	}
	private View.OnClickListener onClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {

			case R.id.breakBtn:
				 finish();
				 overridePendingTransition(R.anim.no_move, R.anim.right_out);
				break;
			default:
				break;
			}
			
		}
	};
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
		{
			finish();
			
			overridePendingTransition(R.anim.no_move, R.anim.right_out);
		}
			break;

		default:
			break;
		}
		
		return super.onKeyDown(keyCode, event);
	}
	

	private void setListView() {
		
		mDataList = new ArrayList<HashMap<String,Object>>();

		mAdapter  = new ConllectAdapter(this, mDataList);
		mAdapter.addInterfaceOnMore(moreInterface);
		listView.setAdapter(mAdapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MyCollectSingleActivity.this, MusicPlayerActivity.class);
				MusicPlayerActivity.mOlineMusicList = mDataList;
				MusicPlayer.position = arg2;
				MusicPlayerActivity.isOnline = true ;
				startActivity(intent);
				
				overridePendingTransition(R.anim.bottom_in,android.R.anim.fade_out);
			}
		});
	}
	
	//添加回调
	private MoreInterface moreInterface = new MoreInterface() {
		
		@Override
		public void OnClickDownload(int position) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void OnClickConllect(int position, Boolean have) {
			HashMap<String, Object> map = mDataList.get(position);
			int index = Integer.parseInt(map.get(ServerAccess.INDEX).toString());
			
			MyCollectSingleActivity.this.index = index;
			
			//删除本地
			DatabaseUtil db = new DatabaseUtil(MyCollectSingleActivity.this);
			db.deleteConllect(index);
			
			//删除网络
			Thread thread = new Thread(delRunnable);
			thread.start();
			
			//刷新
			mDataList.remove(position);
			mAdapter.notifyDataSetChanged();
			
			Toast.makeText(MyCollectSingleActivity.this, "已删除收藏!", Toast.LENGTH_LONG).show();
		}
	};
	
	//获取数据
	//BY Kress
	private void getConllect() {
		new AsyncTask<Integer, Integer, Integer>() 
		{

			@Override
			protected Integer doInBackground(Integer... params) {
				try {
					ServerAccess serverAccess = new ServerAccess(MyCollectSingleActivity.this);
					mTmpList = serverAccess.getConllect(ServerAccess.uid, mPage, 
							new String[]{ServerAccess.MUSIC_NAME, 
							ServerAccess.SINGER, 
							ServerAccess.MUSIC_FILE, 
							ServerAccess.ICON_URL,
							ServerAccess.INDEX});
					
					publishProgress(0);
				} catch (Exception e) {
					// TODO: handle exception
				}
				return null;
			}

			@Override
			protected void onProgressUpdate(
					Integer... values) {
				
				int count = mTmpList.size();
				for(int i=0; i<count; i++) {
					HashMap<String, Object> map = mTmpList.get(i);
					mDataList.add(map);
				}
				
				mAdapter.notifyDataSetChanged();
				mTmpList.clear();
				
				super.onProgressUpdate(values);
			}

			
		}.execute(0);
	}
	
	//删除收藏
		//BY Kress
		protected Runnable delRunnable = new Runnable() {
			
			@Override
			public void run() {
				try {
					ServerAccess serverAccess = new ServerAccess(MyCollectSingleActivity.this);
					serverAccess.delToConllect(index, ServerAccess.uid, new String[]{});

				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		};
}
